<?php
/**
 * Created by PhpStorm.
 * User: acker
 * Date: 21-06-08
 * Time: 下午6:25
 */

namespace App\Models\yb;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Encore\Admin\Traits\DefaultDatetimeFormat;


class UserCouponModel extends Model
{
    use DefaultDatetimeFormat;

    use SoftDeletes;

    /**
     *
     *计量单位
     * @var string
     */
    protected $table = 'yb_user_coupon';

    /**
     * 主键
     *
     * @var string
     */
    protected $primaryKey = 'id';


    /**
     * 该模型是否被自动维护时间戳
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 自定义字段
     *
     * @var bool
     */
    protected $appends = ['coupon_name','full_reduce'];

    /**
     * 优惠券名称
     * @return mixed
     */
    public function getCouponNameAttribute(){
        $coupon_id = $this->coupon_id;
        $coupon_data = CouponModel::where(['id'=>$coupon_id])->first();
        return $coupon_data['name'];
    }

    /**
     * 优惠券名称
     * @return mixed
     */
    public function getFullReduceAttribute(){
        $coupon_id = $this->coupon_id;
        $coupon_data = CouponModel::where(['id'=>$coupon_id])->first();
        $promotion_full = $coupon_data->promotion_full;
        $promotion_reduce = $coupon_data->promotion_reduce;
        return "满".$promotion_full."减".$promotion_reduce;
    }

    /**
     * 关联用户
     * @return mixed
     */
    public function User()
    {
        return $this->hasOne(UserModel::class,'id','user_id');
    }


}
